package company

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"jwgin/model/mysql"
	"jwgin/service"
	"jwgin/utils/salt"
	"jwgin/utils/sign"
	"time"
	"unicode/utf8"
)

type user struct{}

func (l *user) Routers(Router *gin.RouterGroup) (R gin.IRoutes) {
	Router.GET("user", l.get)   //会员列表
	Router.PUT("user", l.put)   //会员添加
	Router.POST("user", l.post) //会员更新
	//Router.DELETE("user", l.del) //会员删除
	return Router
}

func (user) get(c *gin.Context) {
	UserID := c.GetUint("UserID")
	if UserID == 0 {
		RespErr(c, fmt.Errorf("系统用户认证失败"))
		return
	}
	count, size, offset := pages(c)
	db := service.G.Model(mysql.User{}).Where("comid = ? ", UserID)

	//名称
	if username := c.Query("username"); username != "" {
		db = db.Where("username = ?", username)
	}

	//账号
	if account := c.Query("account"); account != "" {
		db = db.Where("user.account = ?", account)
	}

	//状态
	if state := c.Query("state"); state != "" {
		db = db.Where("user.state = ?", state)
	}

	//时间筛选
	startTime := c.Query("startTime")
	endTime := c.Query("endTime")
	if startTime != "" && endTime != "" {
		db = db.Where(" user.addtime > ? AND user.addtime < ? ", startTime, endTime)
	}

	list := []map[string]interface{}{}
	db.Select("user.id,username,user.account,user.bean,user.addtime,user.state,user.uptime").Count(&count).Offset(offset).Limit(size).Scan(&list)
	RespList(c, count, list)
}
func (user) put(c *gin.Context) {

	UserID := c.GetUint("UserID")
	if UserID == 0 {
		RespErr(c, fmt.Errorf("系统用户认证失败"))
		return
	}

	requ := &mysql.User{}
	if err := c.ShouldBind(requ); err != nil {
		RespErr(c, err)
		return
	}

	if utf8.RuneCountInString(requ.Username) > 16 || utf8.RuneCountInString(requ.Username) < 6 {
		RespErr(c, fmt.Errorf("用户名 6-16 位 当前%v", utf8.RuneCountInString(requ.Username)))
		return
	}

	if len(requ.Account) > 16 || len(requ.Account) < 6 {
		RespErr(c, fmt.Errorf("账号 6-16 位"))
		return
	}

	if len(requ.Password) > 16 || len(requ.Password) < 6 {
		RespErr(c, fmt.Errorf("密码6-16位"))
		return
	}

	if requ.State < 1 {
		requ.State = 2
	}

	users := &mysql.User{}
	if err := service.G.Model(mysql.User{}).Where("account = ? ", requ.Account).First(users).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			RespErr(c, err)
			return
		}
	}

	if users.Account == requ.Account {
		RespErr(c, fmt.Errorf("该账号已注册"))
		return
	}
	requ.Comid = int(UserID)
	requ.Salt = salt.SaltStr(6)
	requ.Password = sign.HmacSha256(requ.Salt, requ.Password)
	requ.Addtime = time.Now().Unix()
	requ.Uptime = time.Now().Unix()
	service.G.Create(requ)
	RespSucc(c, nil)
}
func (user) post(c *gin.Context) {
	requ := &mysql.User{}
	if err := c.ShouldBind(requ); err != nil {
		RespErr(c, err)
		return
	}

	UserID := c.GetUint("UserID")
	if UserID == 0 {
		RespErr(c, fmt.Errorf("系统用户认证失败"))
		return
	}

	com := &mysql.User{}
	if err := service.G.Where("id = ? AND comid = ? ", requ.Id, UserID).First(&com).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			RespErr(c, err)
			return
		} else {
			RespErr(c, fmt.Errorf("找不到要修改的用户"))
			return
		}
	}

	if requ.Password != "" {
		password := sign.HmacSha256(com.Salt, requ.Password)
		service.G.Model(&mysql.User{}).Where("id = ? ", requ.Id).Updates(map[string]interface{}{
			"state":    requ.State,
			"password": password,
		})
	} else {
		service.G.Model(&mysql.User{}).Where("id = ? ", requ.Id).Update("state", requ.State)
	}
	RespSucc(c, nil)
}
func (user) del(c *gin.Context) {}
